// page_two/drugcc/drugcc.js
let app = getApp()
let yuming = app.globalData.yuming
let fuhao = app.globalData.fuhao
var numzimu = app.globalData.numzimu
var chaninese = app.globalData.chaninese
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '',
    buttonClicked: false,
    currtab: 0,
    gongwu: false,
    state: '', //0是添加 1是修改
    she_id: '', //设备id
    sbList: [],
    sbname: '',
    sbbh: '',
    scph: '',
    sccs: '',
    sbhgz: '',
    wusbhgz: '',
    sblz: '',
    wusblz: '',
    page: 1,
    yiyao: '',
    humidityid: '', //温湿度的编号
    shebeixq: '', //设备详情
    isxiu: '', //0修改 1查看
    study_status: 0, //冷藏/阴凉（0其他，1冷藏，2阴凉）
    isxuan: false,
    isxuans: false
  },
  xuanz(e) {
    var index = e.currentTarget.dataset.index
    if (this.data.isxiu != 0) {
      this.setData({
        study_status: index
      })
      if (index == 2) {
        this.setData({
          isxuan: true,
          isxuans: false,
        })
      }
      if (index == 1) {
        this.setData({
          isxuans: true,
          isxuan: false,
        })
      }
    }

  },
  qies(e) {
    shuzong = []
    shu = []
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
      page: 1,
      sbname: '',
      sbbh: '',
      scph: '',
      sccs: '',
      sbhgz: '',
      wusbhgz: '',
      sblz: '',
      wusblz: '',
      gongwu: false,
    })
  },
  // 冷链
  lenglian() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Shop/shop_checkout_alllist', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      satrtime: '',
      endtime: ''
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true
            })
          }
        }
        that.setData({
          sbList: shuzong.concat(shu)
        })
      }
    })
  },
  // 设备名称
  sbname(e) {
    this.setData({
      sbname: e.detail.value
    })
  },
  // 设备编号
  sbbh(e) {
    this.setData({
      sbbh: e.detail.value
    })
  },
  // 生产批号
  scph(e) {
    this.setData({
      scph: e.detail.value
    })
  },
  // 生产厂商
  sccs(e) {
    this.setData({
      sccs: e.detail.value
    })
  },
  // 温湿度编号
  humidityid(e) {
    this.setData({
      humidityid: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa
    var that = this;
    //选取图片
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //设备合格证
        if (aaa == 1) {
          that.setData({
            sbhgz: urls,
            wusbhgz: data.imgUrl
          })
        }
        // 设备留证
        if (aaa == 2) {
          that.setData({
            sblz: urls,
            wusblz: data.imgUrl
          })
        }
      })
    })
  },
  // 点击放大
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  // 验证
  ti() {
    if (this.data.sbname == '') {
      wx.showToast({
        title: '请输入设备名称',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.sbname))) {
      wx.showToast({
        title: '设备名称格式错误',
        icon: 'none'
      })
      this.setData({
        sbname: ''
      })
    }
    //  else if (this.data.sbbh == '') {
    //   wx.showToast({
    //     title: '请输入设备编号',
    //     icon: 'none'
    //   })
    // } else if ((fuhao.test(this.data.sbbh))) {
    //   wx.showToast({
    //     title: '设备编号格式错误',
    //     icon: 'none'
    //   })
    //   this.setData({
    //     sbbh: ''
    //   })
    // }
    // else if (chaninese.test(this.data.sbbh) == false) {
    //   wx.showToast({
    //     title: '设备编号不能输入中文!',
    //     icon: 'none'
    //   })
    // }
    // else if (this.data.scph == '') {
    //   wx.showToast({
    //     title: '请输入生产批号',
    //     icon: 'none'
    //   })
    // } 
    // else if ((fuhao.test(this.data.scph))) {
    //   wx.showToast({
    //     title: '生产批号格式错误',
    //     icon: 'none'
    //   })
    //   this.setData({
    //     scph: ''
    //   })
    // } 
    else if (this.data.sccs == '') {
      wx.showToast({
        title: '请输入生产厂商',
        icon: 'none'
      })
    } else if ((fuhao.test(this.data.sccs))) {
      wx.showToast({
        title: '生产厂商格式错误',
        icon: 'none'
      })
      this.setData({
        sccs: ''
      })
    } else if (this.data.humidityid == '') {
      wx.showToast({
        title: '请输入设备编号',
        icon: 'none'
      })
    } else if (numzimu.test(this.data.humidityid) == false) {
      wx.showToast({
        title: '设备编号格式错误!',
        icon: 'none'
      })
    } else if (this.data.study_status == 0) {
      wx.showToast({
        title: '请选择分类',
        icon: 'none'
      })
    } else if (this.data.wusbhgz == '') {
      wx.showToast({
        title: '请上传设备合格证',
        icon: 'none'
      })
    } else if (this.data.wusblz == '') {
      wx.showToast({
        title: '请上传设备留证',
        icon: 'none'
      })
    } else {
      if (wx.getStorageSync('wen_is_ding') == 0) {
        this.dingyue()
      } else {
        this.que()
      }
    }
  },
  // 添加设备
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    var url = '';
    var data = {}
    // state 0添加 1修改
    if (that.data.state == 0) {
      url = 'Yiyao/yiyaoDeviceAdd'
      data = {
        shopId: wx.getStorageSync('shopId'),
        name: that.data.sbname,
        devcode: that.data.sbbh,
        pihao: that.data.scph,
        changshang: that.data.sccs,
        hege_img: that.data.wusbhgz,
        liuzheng_img: that.data.wusblz,
        humidityid: that.data.humidityid,
        shade_cold: that.data.study_status //冷藏/阴凉（0其他，1冷藏，2阴凉）
      }
    }
    if (that.data.state == 1) {
      url = 'Yiyao/yiyaoDeviceSave'
      data = {
        id: that.data.she_id,
        shopId: wx.getStorageSync('shopId'),
        name: that.data.sbname,
        devcode: that.data.sbbh,
        pihao: that.data.scph,
        changshang: that.data.sccs,
        hege_img: that.data.wusbhgz,
        liuzheng_img: that.data.wusblz,
        humidityid: that.data.humidityid,
        shade_cold: that.data.study_status
      }
    }
    this.myRequest(yuming + url, data, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0,
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  dingyue() {
    var that = this
    if (!wx.getStorageSync('token')) {
      wx.showModal({
        title: '订阅监管消息需要授权哦~',
        cancelText: '取消',
        confirmText: '前往',
        success: function (res) {
          if (res.confirm) {
            console.log("确定")
            wx.navigateTo({
              url: '/page_zhu/shou/shou?sta=66',
            })
          } else {
            that.que()
          }
        }
      })
    } else {
      if (wx.getStorageSync('wen_is_ding') == 0) {
        wx.showModal({
          title: '提示',
          content: '请授权开通服务通知',
          showCancel: true,
          success: function (ress) {
            if (ress.confirm) {
              wx.requestSubscribeMessage({ // 调起消息订阅界面
                tmplIds: ['f5VhPUbj51dhls-w-ruVnM5mkZ7HfZ2zqaUps-1tolY', 'f5VhPUbj51dhls-w-ruVnKKomc7IFXExpjFCKrrJwsQ', 'tKyckTTaIqCaJpRAqgrjmyk0CcNwney27x5U-je8VRs'],
                success(res) {
                  console.log(res);
                  if (res["f5VhPUbj51dhls-w-ruVnM5mkZ7HfZ2zqaUps-1tolY"] === "accept") {
                    wx.request({
                      url: yuming + 'Besall/subscribe_save',
                      method: 'POST',
                      header: {
                        "content-type": "application/x-www-form-urlencoded",
                        token: wx.getStorageSync('token'),
                      },
                      data: {

                      },
                      success: function (res) {
                        console.log(res)
                        if (res.data.code == 200) {
                          wx.setStorageSync('wen_is_ding', 1)
                          that.que()
                        } else {
                          wx.setStorageSync('wen_is_ding', 1)
                          that.que()
                        }
                      },
                      fail: function () {
                        console.log("请求数据失败");
                      }
                    })
                  }


                },
                fail(er) {
                  console.log("订阅消息 失败 ");
                  console.log(er);
                  wx.request({
                    url: yuming + 'Besall/subscribe_save',
                    method: 'POST',
                    header: {
                      "content-type": "application/x-www-form-urlencoded",
                      token: wx.getStorageSync('token'),
                    },
                    data: {

                    },
                    success: function (res) {
                      console.log(res)
                      if (res.data.code == 200) {
                        wx.setStorageSync('wen_is_ding', 1)
                        that.que()
                      } else {
                        wx.setStorageSync('wen_is_ding', 1)
                        that.que()
                      }
                    },
                    fail: function () {
                      console.log("请求数据失败");
                    }
                  })
                  wx.setStorageSync('wen_is_ding', 0)
                }
              })

            } else {
              that.que()
            }
          }
        })
      } else {
        that.jiek()
      }

    }
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      shopType: options.shopType,
      title: options.title,
      state: options.state,
      isxiu: options.isxiu
    })
    if (options.state == 1) {
      this.setData({
        she_id: options.she_id,
      })
    }
    this.setData({
      yiyao: wx.getStorageSync('stryId')
    })
    if (this.data.state == 1) {
      var shebeixq = wx.getStorageSync('shebeixq')
      this.setData({
        shebeixq: shebeixq,
        sbname: shebeixq.name,
        sbbh: shebeixq.devcode,
        sccs: shebeixq.changshang,
        humidityid: shebeixq.humidityid,
        sbhgz: shebeixq.hege_img,
        sblz: shebeixq.liuzheng_img,
        study_status: shebeixq.shade_cold.toString(),
      })


      if(shebeixq.hege_img != '' && shebeixq.hege_img != null){
        this.setData({
          wusbhgz: shebeixq.hege_img.slice(23) ,
        })
      }
      if(shebeixq.liuzheng_img != '' && shebeixq.liuzheng_img != null){
        this.setData({
          wusblz: shebeixq.liuzheng_img.slice(23),
        })
      }
      if (this.data.study_status == 2) {
        this.setData({
          isxuan: true
        })
      } else {
        this.setData({
          isxuan: false
        })
      }
      if (this.data.study_status == 1) {
        this.setData({
          isxuans: true
        })
      } else {
        this.setData({
          isxuans: false
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})